<?php
/**
 * 触发邮件模块
 */
 
namespace app\controllers\marketing;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\data\Pagination;
use app\components\TemplateMarket;
use app\models\marketing\MailTemplate;
use app\models\marketing\MailAutoCreate;
use app\models\marketing\Region;
use app\models\marketing\ExclusivePriceConfig;
use app\models\marketing\ExclusivepriceOperLog;
use yii\helpers\Html;

class TriggerMailManageController extends \app\base\Controller
{
    public $enableCsrfValidation = false;
    
    private $nowAddr = '触发邮件管理';
    /**
     * 触发邮件配置列表
     */
    public function actionList()
    {
        $data = [];
        $siteInfo = [];
        $defaultPageSize = 20;
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
       
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        $siteInfo[0] = "全部";
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
       
        $formData['siteInfo'] = $siteInfo;
        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0;
        
        // 模板名称/模板ID搜索条件处理
        if (!empty($formData['template_info'])) {
            if(is_numeric($formData['template_info'])) {
                $formData['template_id'] =  $formData['template_info'];
            } else {
                $formData['template_name'] = addslashes($formData['template_info']);
            }
        }
        
        // 邮件状态
        $formData['state'] = isset($formData['state']) ? intval($formData['state']) : -1;
       
        // 分页参数相关
        $page = Yii::$app->request->$mothod('page') ? Yii::$app->request->$mothod('page') : 1;
        $pageSize = Yii::$app->request->$mothod('pagesize') ? Yii::$app->request->$mothod('pagesize') : $defaultPageSize;
        
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
        // 获取自动触发邮件的配置数据
        $mailAutoCreate = new MailAutoCreate();
       
        $dataSelect = $mailAutoCreate->getAutoCreateList($formData);
       
        $totalCount = $dataSelect['totalCount'];
        
        // 分页相关
        $pages = new Pagination(['totalCount' =>$totalCount, 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
        return $this->render('//marketing//trigger-mail-manage/list',[
            'data' => $dataSelect['mail_auto_create_info'],
            'formData' => $formData,
            'pages' => $pages,
        ]);
    }
    
    /*
     * 专享价配置列表
     * @author liugang 2018-3-12
     */
    public function actionExclusivePriceConfigList() 
    {
        $data = [];
        $siteInfo = [];
        $defaultPageSize = 20;
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        $siteInfo[0] = "全部";
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
       
        $formData['siteInfo'] = $siteInfo;
        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0;
        
        // 获取模版类型
        $TemplateMarket = new TemplateMarket();
        if (!empty($formData['site_id'])) {
            $templateTypeArr = $TemplateMarket->getTemplateType($formData['site_id']);
        } else {
            $templateTypeArr = $TemplateMarket->getTemplateType();
        }
        
        $templateTypeArrInfo = [];
        $formData['tempType'] = [];
        //$formData['temp_type_id'] = 0;
        $templateTypeArrInfo[0] = "全部";
        if (isset($templateTypeArr) && is_array($templateTypeArr)) {
            foreach ($templateTypeArr as $templateTypeArrK => $templateTypeArrV) {
                $templateTypeArrInfo[$templateTypeArrV['id']] = $templateTypeArrV['template_type_name'];
               
            } 
            //$formData['temp_type_id'] = $templateTypeArr[0]['id'];
            $formData['tempType'] = $templateTypeArrInfo;
        }
        
        // 模板邮件类型搜索条件处理
        $formData['temp_type_id'] = isset($formData['temp_type_id']) ? intval($formData['temp_type_id']) : 0;
        $formData['type_id'] = $formData['temp_type_id'];
        
        // 分页参数相关
        $page = Yii::$app->request->$mothod('page') ? Yii::$app->request->$mothod('page') : 1;
        $pageSize = Yii::$app->request->$mothod('pagesize') ? Yii::$app->request->$mothod('pagesize') : $defaultPageSize;
        
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
        // 获取自动触发邮件的配置数据
        $exclusivePriceConfig = new ExclusivePriceConfig();
       
        $data = $exclusivePriceConfig->getExclusivePriceConfigList($formData);
       
        $totalCount = $data['totalCount'];
        // 分页相关
        $pages = new Pagination(['totalCount' =>$totalCount, 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
        return $this->render('//marketing//trigger-mail-manage/exclusive-price-list',[
            'data' => $data['exclusive_price_config_info'],
            'formData' => $formData,
            'pages' => $pages,
        ]);
    }
    
    /*
     * 查看配置对应的商品详情
     * @author liugang 2018-3-15
     */
    public function actionExclusivePriceConfigGoodsDetail()
    {
        $data = [];
        $defaultPageSize = 20;
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        $formData['id'] = !empty($formData['id']) ? intval($formData['id']) : 0;
        
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
        if(!$formData['id']) {
            $this->sysMsg('警告', "参数不正确",
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        $formData['configStatus'] = [
            0  => '全部',
            1  => '设置成功',
            2  => '设置失败'
        ];
        
        $formData['status'] = !empty($formData['status']) ? intval($formData['status']) : 0;
        $formData['good_sn'] = !empty($formData['good_sn']) ? trim($formData['good_sn']) : '';
        
        // 默认的时间数据
        if (!empty($formData['reservation_time'])){
            $reservationTimeArr = explode(' - ',$formData['reservation_time']);
            $formData['startTime'] = $reservationTimeArr[0];
            $formData['endTime'] = $reservationTimeArr[1];
        } else {
            // 当前时间 
            $curTime = \yii::$app->bjTime->time();
            $formData['startTime'] = \yii::$app->bjTime->date('Y-m-d',$curTime-30*24*60*60);
            $formData['endTime'] = \yii::$app->bjTime->date('Y-m-d',$curTime);
        }
        
        // 分页参数相关
        $page = !empty($formData['page']) ? $formData['page'] : 1;
        $pageSize = !empty($formData['pagesize']) ? $formData['pagesize'] : $defaultPageSize;
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
        
        // 获取自动触发邮件的配置数据
        $exclusivePriceConfig = new ExclusivePriceConfig();
       
        $data = $exclusivePriceConfig->getExclusivePriceGoodsList($formData);
       
        $totalCount = $data['totalCount'];
        
        // 分页相关
        $pages = new Pagination(['totalCount' =>$totalCount, 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
        return $this->render('//marketing//trigger-mail-manage/exclusive-price-config-detail-list',[
            'data' => $data['exclusive_price_goods_detail'],
            'formData' => $formData,
            'pages' => $pages,
        ]);
    }
    
    
    /**
     * 新增配置
     */
    public function actionAdd()
    {
        $data = [];
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        if (empty($siteInfo)) {
            exit('你好，你没有任何网站的相关权限');
        }
        $formData['siteInfo'] = $siteInfo;
        $formData['site_id'] = !empty($formData['site_id']) ? (int)$formData['site_id'] : array_keys($siteInfo)[0];
        
        // 获取当前这一个网站的启动的自动触发的邮件模板信息
        $formData['templateInfo'] = [];
        $formData['templateInfo'][0] = "--选择邮件模板--";
        $template = new \app\models\marketing\MailTemplate();
        $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);  
        foreach ($dataTmp as $dataK => $dataV) {
            $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
        }  
        
        $formData['templateInfoAll'] = [];
        $formData['templateInfoAll'][0] = "--选择邮件模板--";
        // 获取所有启用状态的模版
        $dataAllTmp = $template->getTemplateDetailBySite($formData['site_id']);
           
        foreach ($dataAllTmp as $dataK => $dataV) {
            $formData['templateInfoAll'][$dataV['template_id']] = $dataV['template_name'];
        }
        
        // 获取国家信息
        $region = new Region;
        $regionInfo = $region->getRegionCode();
        $formData['region_info'] = ['' => '全部'];
        foreach($regionInfo as $region) {
            $formData['region_info'][$region['region_code']] = $region['region_name'];
        }
        
        $dataAutoType = $template->getAutoTriggerTemplateType($formData['site_id']);
        $formData['auto_type'] = [];
        
        foreach ($dataAutoType  as $dataK => $dataV) {
            $formData['auto_type'][$dataV['id']] = $dataV['template_type_name'];
        }

        return $this->render('//marketing//trigger-mail-manage/add',[
            'data' => $data,
            'formData' => $formData,
        ]);
    }
    
    /*
     *  新增专享价配置
     *  user: liugang
     *  date: 2018-3-14 
     */
    public function actionExclusivePriceConfigAdd()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
       
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        
        $formData['siteInfo'] = $siteInfo;
        $formData['site_id'] = array_keys($siteInfo)[0];
        
        $templateIds = [];
        $exclusivePriceConfig = new ExclusivePriceConfig();
        
        
        // 获取已经配置过专享价的邮件模板id
        if($formData['site_id']) {
            
            $templateIdsArr = $exclusivePriceConfig->getAllTemplates($formData['site_id']);
            foreach($templateIdsArr as $templateId) {
                $templateIds[] = $templateId['template_id'];
            }
            $templateIds = array_unique($templateIds);
            $site_name = Yii::$app->params['sites']['marketing_email']['web_index'][$formData['site_id']];
        }
        
        // 获取当前这一个网站的启动的自动触发的并没有配置专享价的邮件模板信息
        $formData['templateInfo'] = $formData['templateOther'] = [];
        $formData['templateInfo'][0] = "--选择邮件模板--";
        $template = new \app\models\marketing\MailTemplate();
        $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);  
        foreach ($dataTmp as $dataK => $dataV) {
            // 剔除掉已经配置过的模板
            if(!in_array($dataV['template_id'],$templateIds)) {
                $brief_code = isset($site_name) && isset(Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code']) ? Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code'] : '';
                
                $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
                $formData['templateOther'][$dataV['template_id']] = $brief_code.$dataV['template_id'].','.$dataV['template_type_compaign'];
            }
        }  
        
        // 获取站点的渠道数据
        $channelInfo = $exclusivePriceConfig->getChannels($formData['site_id']);
        $formData['channelInfo'][0] = "--选择渠道--";
        foreach($channelInfo as $key => $val) {
            $formData['channelInfo'][$val['id']] = $val['channel_name'];
        }
        
        // 获取国家数据
        $regionInfo = $exclusivePriceConfig->getRegions();
         $formData['regionInfo'][0] = "--选择国家--";
        foreach($regionInfo as $key => $val) {
            $formData['regionInfo'][$val['region_id']] = $val['region_name'];
        }
        
        return $this->render('//marketing//trigger-mail-manage/exclusive-price-config-add',[
            'formData' => $formData,
            'site' => $site,
        ]);
    }
    
    /*
     *  根据站点ID获取相应的数据
     *  user: liugang
     *  date: 2018-3-20 
     */
    public function actionExclusivePriceConfigAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
       
        $formData['site_id'] = !empty($formData['site_id']) ? (int)$formData['site_id'] : 0;
        
        $templateIds = $returnData = [];
        
        if(!$formData['site_id']) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "参数不正确";
            echo json_encode($returnData);
            exit;
        }
        
        $exclusivePriceConfig = new ExclusivePriceConfig();
        
        
        // 获取已经配置过专享价的邮件模板id
        if($formData['site_id']) {
            
            $templateIdsArr = $exclusivePriceConfig->getAllTemplates($formData['site_id']);
            foreach($templateIdsArr as $templateId) {
                $templateIds[] = $templateId['template_id'];
            }
            $templateIds = array_unique($templateIds);
            $site_name = Yii::$app->params['sites']['marketing_email']['web_index'][$formData['site_id']];
        }
        
        // 获取当前这一个网站的启动的自动触发的并没有配置专享价的邮件模板信息
        $formData['templateInfo'] = $formData['templateOther'] = [];
        $formData['templateInfo'][0] = "--选择邮件模板--";
        $template = new \app\models\marketing\MailTemplate();
        $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);  
        foreach ($dataTmp as $dataK => $dataV) {
            // 剔除掉已经配置过的模板
            if(!in_array($dataV['template_id'],$templateIds)) {
                $brief_code = isset($site_name) && isset(Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code']) ? Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code'] : '';
                
                $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
                $formData['templateOther'][$dataV['template_id']] = $brief_code.$dataV['template_id'].','.$dataV['template_type_compaign'];
            }
        }  
        
        // 获取站点的渠道数据
        $channelInfo = $exclusivePriceConfig->getChannels($formData['site_id']);
        $formData['channelInfo'][0] = "--选择渠道--";
        foreach($channelInfo as $key => $val) {
            $formData['channelInfo'][$val['id']] = $val['channel_name'];
        }
        
        // 获取国家数据
        $regionInfo = $exclusivePriceConfig->getRegions();
        $formData['regionInfo'][0] = "--选择国家--";
        foreach($regionInfo as $key => $val) {
            $formData['regionInfo'][$val['region_id']] = $val['region_name'];
        }
        
        $formData['templateInfo'] = Html::dropDownList('template_id',0, $formData['templateInfo'], ['class' => 'form-control z-select mr10 mb5']);
        $formData['templateOther'] = Html::dropDownList('template_other',0, $formData['templateOther'], ['class' => 'form-control z-select mr10 mb5']);
        $formData['channelInfo'] = Html::dropDownList('channel_id',0, $formData['channelInfo'], ['class' => 'form-control z-select mr10 mb5']);
        $formData['regionInfo'] = Html::dropDownList('region_id',0, $formData['regionInfo'], ['class' => 'form-control z-select mr10 mb5']);
        
        $returnData['status'] = 0;
        $returnData['errorInfo'] = "success";
        $returnData['formData'] = $formData;
        echo json_encode($returnData);
        
    }
    
    /*
     *  根据站点ID获取相应的数据
     *  user: liugang
     *  date: 2018-3-20 
     */
    public function actionExclusivePriceConfigEditAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
       
        $formData['site_id'] = !empty($formData['site_id']) ? (int)$formData['site_id'] : 0;
        $editConfigId = !empty($formData['id']) ? (int)$formData['id'] : 0;
        $editSiteId = !empty($formData['edit_site_id']) ? (int)$formData['edit_site_id'] : 0;
        
        $templateIds = $returnData = $configInfo = [];
        
        if(!$formData['site_id']) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "参数不正确";
            echo json_encode($returnData);
            exit;
        }
        
        
        $exclusivePriceConfig = new ExclusivePriceConfig();
        if($editConfigId && $editSiteId) {
            // 获取当前配置信息
            $configInfo = $exclusivePriceConfig->getOne($editConfigId);
        }
        
        
        
        // 获取已经配置过专享价的邮件模板id
        if($formData['site_id']) {
            
            $templateIdsArr = $exclusivePriceConfig->getAllTemplates($formData['site_id']);
            foreach($templateIdsArr as $templateId) {
                $templateIds[] = $templateId['template_id'];
            }
            $templateIds = array_unique($templateIds);
            $site_name = Yii::$app->params['sites']['marketing_email']['web_index'][$formData['site_id']];
        }
        
        // 获取当前这一个网站的启动的自动触发的并没有配置专享价的邮件模板信息
        $formData['templateInfo'] = $formData['templateOther'] = [];
        $formData['templateInfo'][0] = "--选择邮件模板--";
        $template = new \app\models\marketing\MailTemplate();
        $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);  
        foreach ($dataTmp as $dataK => $dataV) {
            // 剔除掉已经配置过的模板
            if(!in_array($dataV['template_id'],$templateIds) || ($editSiteId && $formData['site_id'] == $editSiteId && isset($configInfo['template_id']) && $dataV['template_id'] == $configInfo['template_id'])) {
                $brief_code = isset($site_name) && isset(Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code']) ? Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code'] : '';
                
                $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
                $formData['templateOther'][$dataV['template_id']] = $brief_code.$dataV['template_id'].','.$dataV['template_type_compaign'];
            }
        }  
        
        // 获取站点的渠道数据
        $channelInfo = $exclusivePriceConfig->getChannels($formData['site_id']);
        $formData['channelInfo'][0] = "--选择渠道--";
        foreach($channelInfo as $key => $val) {
            $formData['channelInfo'][$val['id']] = $val['channel_name'];
        }
        
        // 获取国家数据
        $regionInfo = $exclusivePriceConfig->getRegions();
        $formData['regionInfo'][0] = "--选择国家--";
        foreach($regionInfo as $key => $val) {
            $formData['regionInfo'][$val['region_id']] = $val['region_name'];
        }
        
        $formData['templateInfo'] = Html::dropDownList('template_id',0, $formData['templateInfo'], ['class' => 'form-control z-select mr10 mb5']);
        $formData['templateOther'] = Html::dropDownList('template_other',0, $formData['templateOther'], ['class' => 'form-control z-select mr10 mb5']);
        $formData['channelInfo'] = Html::dropDownList('channel_id',0, $formData['channelInfo'], ['class' => 'form-control z-select mr10 mb5']);
        $formData['regionInfo'] = Html::dropDownList('region_id',0, $formData['regionInfo'], ['class' => 'form-control z-select mr10 mb5']);
        
        $returnData['status'] = 0;
        $returnData['errorInfo'] = "success";
        $returnData['formData'] = $formData;
        echo json_encode($returnData);
        
    }
    
    
    /*
     *  编辑专享价配置
     *  user: liugang
     *  date: 2018-3-14 
     */
    public function actionExclusivePriceConfigEdit()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        $editConfigId = $formData['id'] ? intval($formData['id']) : 0;
        
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
        if(!$editConfigId) {
            $this->sysMsg('警告', "参数不正确",
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        
        $formData['siteInfo'] = $siteInfo;
        
        
        $templateIds = [];
        $exclusivePriceConfig = new ExclusivePriceConfig();
        
        // 获取当前配置信息
        $configInfo = $exclusivePriceConfig->getOne($editConfigId);
        
        if(!$configInfo) {
            $this->sysMsg('警告', "要修改的配置数据不存在",
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        $formData['site_id'] = $configInfo['site_id'];
        
        // 获取已经配置过专享价的邮件模板id
        if($formData['site_id']) {
            $templateIdsArr = $exclusivePriceConfig->getAllTemplates($formData['site_id']);
            foreach($templateIdsArr as $templateId) {
                $templateIds[] = $templateId['template_id'];
            }
            $templateIds = array_unique($templateIds);
            $site_name = Yii::$app->params['sites']['marketing_email']['web_index'][$formData['site_id']];
        }
        
        // 获取当前这一个网站的启动的自动触发的并没有配置专享价的邮件模板信息
        $formData['templateInfo'] = $formData['templateOther'] = [];
        $formData['templateInfo'][0] = "--选择邮件模板--";
        $template = new \app\models\marketing\MailTemplate();
        $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);  
        
        foreach ($dataTmp as $dataK => $dataV) {
            // 剔除掉已经配置过的模板
            if(!in_array($dataV['template_id'],$templateIds) || $dataV['template_id'] == $configInfo['template_id']) {
                $brief_code = isset($site_name) && isset(Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code']) ? Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code'] : '';
                
                $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
                $formData['templateOther'][$dataV['template_id']] = $brief_code.$dataV['template_id'].','.$dataV['template_type_compaign'];
            }
//            $brief_code = isset($site_name) && isset(Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code']) ? Yii::$app->params['sites']['marketing_email']['web_config'][$site_name]['brief_code'] : '';
//                
//            $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
//            $formData['templateOther'][$dataV['template_id']] = $brief_code.$dataV['template_id'].','.$dataV['template_type_compaign'];
        }  
        
        // 获取站点的渠道数据
        $channelInfo = $exclusivePriceConfig->getChannels($formData['site_id']);
        $formData['channelInfo'][0] = "--选择渠道--";
        foreach($channelInfo as $key => $val) {
            $formData['channelInfo'][$val['id']] = $val['channel_name'];
        }
        
        // 获取国家数据
        $regionInfo = $exclusivePriceConfig->getRegions();
         $formData['regionInfo'][0] = "--选择国家--";
        foreach($regionInfo as $key => $val) {
            $formData['regionInfo'][$val['region_id']] = $val['region_name'];
        }
        
        return $this->render('//marketing//trigger-mail-manage/exclusive-price-config-edit',[
            'formData' => $formData,
            'data' => $configInfo,
            'site' => $site,
        ]);
    }
    
    /**
     * 编辑配置
     */
    public function actionEdit()
    {
        $data = [];
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getMarketEmailAccessSite();
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        if (empty($siteInfo)) {
            exit('你好，你没有任何网站的相关权限');
        }
        $formData['siteInfo'] = $siteInfo;
        
        if (empty($formData['id'])) {
            exit('你好，没有传递相关配置的id值');
        } else {
            $formData['id'] = intval($formData['id']);
        } 
        
        // 获取当前id对应配置的相关信息
        $mailAutoCreate = new MailAutoCreate();
        $curMailAutoCreateInfoAll = $mailAutoCreate->getOneAutoCreateAllById($formData['id']);
        
        // 判断当前网站的触发任务是否有权限更改
        $flag = Yii::$app->authority->judgeMarketEmailHaveSite($curMailAutoCreateInfoAll['site_id']);
        if (!$flag) {
            exit('您的帐号没有相关权限!');
        } 
        
        // 当前配置所有网站
        $formData['site_id'] = $curMailAutoCreateInfoAll['site_id'];
        
        // 获取当前这一个网站的启动的自动触发的邮件模板信息
        $formData['templateInfo'] = [];
        $formData['templateInfo'][0] = "--选择邮件模板--";
        $template = new \app\models\marketing\MailTemplate();
        $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);  
        foreach ($dataTmp as $dataK => $dataV) {
            $formData['templateInfo'][$dataV['template_id']] = $dataV['template_name'];
        } 
        
        $formData['templateInfoAll'] = [];
        $formData['templateInfoAll'][0] = "--选择邮件模板--";
        // 获取所有启用状态的模版
        $dataAllTmp = $template->getTemplateDetailBySite($formData['site_id']);
           
        foreach ($dataAllTmp as $dataK => $dataV) {
            $formData['templateInfoAll'][$dataV['template_id']] = $dataV['template_name'];
        }
        
        // 获取国家信息
        $region = new Region;
        $regionInfo = $region->getRegionCode();
        $formData['region_info'] = [];
        foreach($regionInfo as $region) {
            $formData['region_info'][$region['region_code']] = $region['region_name'];
        }
        
        // 自动触发邮件模版类型
        $dataAutoType = $template->getAutoTriggerTemplateType($formData['site_id']);
        $formData['auto_type'] = [];
        
       
        foreach ($dataAutoType  as $dataK => $dataV) {
            $formData['auto_type'][$dataV['id']] = $dataV['template_type_name'];
        }
        
        // 当前配置选择邮件模版
        $formData['template_id'] = $curMailAutoCreateInfoAll['template_id'];
        
        // 当前计划时间
        $formData['plan_send_time'] = $curMailAutoCreateInfoAll['plan_send_time'];
        
        // 当前发送方式
        $formData['send_mode_id'] = $curMailAutoCreateInfoAll['send_mode_id'];
        
        // 当前延后时间
        $formData['delay_day'] = $curMailAutoCreateInfoAll['delay_day'];

        // 当前过滤条件
        if (!empty($curMailAutoCreateInfoAll['filter_con'])) {
            $formData['filter_con'] = json_decode($curMailAutoCreateInfoAll['filter_con'],true);
        } else {
            $formData['filter_con'] = [];
        }
       
        // 当前订阅过滤条件
        if (!empty($curMailAutoCreateInfoAll['must_received'])) {
            $formData['must_received'] = json_decode($curMailAutoCreateInfoAll['must_received'],true);
        } else {
            $formData['must_received'] = [];
        } 
        
        $formData['select_region_condition'] = json_decode($curMailAutoCreateInfoAll['select_region_condition'],true);
        $formData['filter_region_condition'] = json_decode($curMailAutoCreateInfoAll['filter_region_condition'],true);
        
        // 当前接口格式
        $formData['data_format'] = $curMailAutoCreateInfoAll['data_format'];
        
        return $this->render('//marketing//trigger-mail-manage/edit',[
            'data' => $data,
            'formData' => $formData,
        ]);
    }
    /**
     * 查看配置
     */
    public function actionDetail()
    {
        $data = [];
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        
        // 根据id获取相关模版的数据
        if (empty($formData['id'])) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        } else {
            // 查看当前id对应自动触发邮件配置的相关信息
            $formData['id'] = intval($formData['id']);
            $mailAutoCreate = new MailAutoCreate();
            $curMailAutoCreateInfoAll = $mailAutoCreate->getOneAutoCreateAllById($formData['id']);
        }
       
        
        return $this->render('//marketing//trigger-mail-manage/detail',[
            'data' => $curMailAutoCreateInfoAll,
            'formData' => $formData,
        ]);
    }
    
    /**
     * 启动任务相关
     */
    public function actionStartAjax()
    {
        
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        
        // 返回信息
        $returnData = [];
        if (empty($formData['id'])) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        } else {
            
            // 查看当前id对应自动触发邮件配置的相关信息
            $formData['id'] = intval($formData['id']);
            $mailAutoCreate = new MailAutoCreate();
            $curMailAutoCreateInfo = $mailAutoCreate->getOneAutoCreateById($formData['id']);
            
            // 查看当前网站的数据有没有相关权限
            if (empty($curMailAutoCreateInfo)) {
                $returnData['status'] = 1;
                $returnData['errorInfo'] = "数据不存在!";
                echo json_encode($returnData);
                exit; 
            } else {
                
                $flag = Yii::$app->authority->judgeMarketEmailHaveSite($curMailAutoCreateInfo['site_id']);
                if (!$flag) {
                    $returnData['status'] = 1;
                    $returnData['errorInfo'] = "你的账号该没有权限!";
                    echo json_encode($returnData);
                    exit;
                } else {
                    $mailAutoCreate->startOneAutoCreateById($formData['id']);
                }
            }
        }
        
        
        $returnData['status'] = 0;
        $returnData['errorInfo'] = "success";
        echo json_encode($returnData);
        exit;
    }
    
    /*
     * 启动邮件专享价配置
     * @author liugang
     * @date 2018-3-14 
     */
    public function actionStartExclusivePriceAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $id = Yii::$app->request->$mothod('id') ? Yii::$app->request->$mothod('id') : 0;
        $startId = intval($id);
        
        // 返回数据初始化
        $returnData = [];
        if(!$startId) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        }
        
        $exclusivePriceConfig = new ExclusivePriceConfig();
        
        $getOne = $exclusivePriceConfig->start($startId);
        
        if($getOne) {
            
            // 添加启动日志
            $exclusivePriceOperLog = new ExclusivepriceOperLog();
            $session = Yii::$app->session;
            $log['oper'] = $session['admin_info']['username'];
            $log['operation'] = '启动';
            $log['site_name'] = isset(Yii::$app->params['sites']['marketing_email']['web_index'][$getOne['site_id']]) ? Yii::$app->params['sites']['marketing_email']['web_index'][$getOne['site_id']] : '';
            $log['template_id'] = $getOne['template_id'];
            $exclusivePriceOperLog->addLog($log);
            
            $returnData['status'] = 0;
            $returnData['errorInfo'] = "success";
            echo json_encode($returnData);
            exit;
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "数据不存在或启动失败!";
            echo json_encode($returnData);
            exit;
        }
    }
    
    /*
     * 查看专享价配置信息
     * @author liugang
     * @date 2018-3-15 
     */
    public function actionExclusivePriceConfigDetail()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $id = Yii::$app->request->$mothod('id') ? Yii::$app->request->$mothod('id') : 0;
        $configId = intval($id);
        
        Yii::$app->params['now_addr'] = $this->nowAddr;
        
        if(!$configId) {
            $this->sysMsg('警告', "参数不正确",
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        $exclusivePriceConfig = new ExclusivePriceConfig();
        $configInfo = $exclusivePriceConfig->getOne($configId);
        if(!$configInfo) {
            $this->sysMsg('警告', "配置信息不存在",
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        
        // 获取对应id的模板名称
        $templateMode = new MailTemplate();
        $templateInfo = $templateMode->getTemplateById($configInfo['template_id']);
        $configInfo['template_name'] = isset($templateInfo['template_name']) ? $templateInfo['template_name'] : '';
        
        // 获取对应渠道id的渠道名称
        $channel = $exclusivePriceConfig->getChannelById($configInfo['channel_id']);
        $configInfo['channel_name'] = isset($channel['channel_name']) ? $channel['channel_name'] : '';
        
        // 获取对应国家id的国家名称
        $region = $exclusivePriceConfig->getRegionById($configInfo['region_id']);
        $configInfo['region_name'] = isset($region['region_name']) ? $region['region_name'] : '— —';
        
        $configInfo['site_name'] = isset(Yii::$app->params['sites']['marketing_email']['web_index'][$configInfo['site_id']]) ? Yii::$app->params['sites']['marketing_email']['web_index'][$configInfo['site_id']] : '';
        
        return $this->render('//marketing//trigger-mail-manage/exclusive-price-config-detail',[
            'data' => $configInfo,
        ]);
    }
    
    
    /*
     * 暂停邮件专享价配置
     * @author liugang
     * @date 2018-3-14 
     */
    public function actionPauseExclusivePriceAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $id = Yii::$app->request->$mothod('id') ? Yii::$app->request->$mothod('id') : 0;
        $pauseId = intval($id);
        
        // 返回数据初始化
        $returnData = [];
        if(!$pauseId) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        }
        
        $exclusivePriceConfig = new ExclusivePriceConfig();
        
        $getOne = $exclusivePriceConfig->pause($pauseId);
        
        if($getOne) {
            
            // 添加启动日志
            $exclusivePriceOperLog = new ExclusivepriceOperLog();
            $session = Yii::$app->session;
            $log['oper'] = $session['admin_info']['username'];
            $log['operation'] = '暂停';
            $log['site_name'] = isset(Yii::$app->params['sites']['marketing_email']['web_index'][$getOne['site_id']]) ? Yii::$app->params['sites']['marketing_email']['web_index'][$getOne['site_id']] : '';
            $log['template_id'] = $getOne['template_id'];
            $exclusivePriceOperLog->addLog($log);
            
            $returnData['status'] = 0;
            $returnData['errorInfo'] = "success";
            echo json_encode($returnData);
            exit;
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "数据不存在或暂停失败!";
            echo json_encode($returnData);
            exit;
        }
    }
    /*
     * 删除邮件专享价配置
     * @author liugang
     * @date 2018-3-14 
     */
    public function actionDelExclusivePriceAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $id = Yii::$app->request->$mothod('id') ? Yii::$app->request->$mothod('id') : 0;
        $delId = intval($id);
        
        // 返回数据初始化
        $returnData = [];
        if(!$delId) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        }
        $exclusivePriceConfig = new ExclusivePriceConfig();
        $getOne = $exclusivePriceConfig->del($delId);
        if($getOne) {
            
            // 添加日志
            $exclusivePriceOperLog = new ExclusivepriceOperLog();
            $session = Yii::$app->session;
            $log['oper'] = $session['admin_info']['username'];
            $log['operation'] = '删除';
            $log['site_name'] = isset(Yii::$app->params['sites']['marketing_email']['web_index'][$getOne['site_id']]) ? Yii::$app->params['sites']['marketing_email']['web_index'][$getOne['site_id']] : '';
            $log['template_id'] = $getOne['template_id'];
            $exclusivePriceOperLog->addLog($log);
            
            $returnData['status'] = 0;
            $returnData['errorInfo'] = "success";
            echo json_encode($returnData);
            exit;
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "数据不存在或删除失败!";
            echo json_encode($returnData);
            exit;
        }
    }
    /**
     * 启动任务相关
     */
    public function actionPauseAjax()
    {
        
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        
        // 返回信息
        $returnData = [];
        if (empty($formData['id'])) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        } else {
            
            // 查看当前id对应自动触发邮件配置的相关信息
            $formData['id'] = intval($formData['id']);
            $mailAutoCreate = new MailAutoCreate();
            $curMailAutoCreateInfo = $mailAutoCreate->getOneAutoCreateById($formData['id']);
            
            // 查看当前网站的数据有没有相关权限
            if (empty($curMailAutoCreateInfo)) {
                $returnData['status'] = 1;
                $returnData['errorInfo'] = "数据不存在!";
                echo json_encode($returnData);
                exit; 
            } else {
                $flag = Yii::$app->authority->judgeMarketEmailHaveSite($curMailAutoCreateInfo['site_id']);
                if (!$flag) {
                    $returnData['status'] = 1;
                    $returnData['errorInfo'] = "你的账号该没有权限!";
                    echo json_encode($returnData);
                    exit;
                } else {
                    $mailAutoCreate->pauseOneAutoCreateById($formData['id']);
                }
            }
        }
        
        $returnData['status'] = 0;
        $returnData['errorInfo'] = "success";
        echo json_encode($returnData);
        exit;
    }
    
    
    /**
     * 删除某一配置项
     */
    public function actionDelAjax()
    {
        
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        
        // 返回信息
        $returnData = [];
        if (empty($formData['id'])) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id!";
            echo json_encode($returnData);
            exit;
        } else {
            
            // 查看当前id对应自动触发邮件配置的相关信息
            $formData['id'] = intval($formData['id']);
            $mailAutoCreate = new MailAutoCreate();
            $curMailAutoCreateInfo = $mailAutoCreate->getOneAutoCreateById($formData['id']);
            
            // 查看当前网站的数据有没有相关权限
            if (empty($curMailAutoCreateInfo)) {
                $returnData['status'] = 1;
                $returnData['errorInfo'] = "数据不存在!";
                echo json_encode($returnData);
                exit; 
            } else {
                
                $flag = Yii::$app->authority->judgeMarketEmailHaveSite($curMailAutoCreateInfo['site_id']);
                if (!$flag) {
                    $returnData['status'] = 1;
                    $returnData['errorInfo'] = "你的账号该没有权限!";
                    echo json_encode($returnData);
                    exit;
                } else {
                    $affectNum = $mailAutoCreate->delOneAutoCreateById($formData['id']);
                    
                    if ($affectNum) {
                        $returnData['status'] = 0;
                        $returnData['errorInfo'] = "success";
                    } else {
                        $returnData['status'] = 1;
                        $returnData['errorInfo'] = "当前数据不可删除";
                    }
                }
            }
        }
        
        
       
        echo json_encode($returnData);
        exit;
    }
    /**
     * 提交编辑配置信息
     */
    public function actionDoEditAjax()
    {
        
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        
        // 返回信息
        $returnData = [];

        /********************** 处理数据开始 ***************/
        
        // 国家筛选
        $formData['select_region_condition'] = is_array($formData['select_region_condition']) ? array_filter($formData['select_region_condition']) : $formData['select_region_condition'];
        $formData['filter_region_condition'] = is_array($formData['filter_region_condition']) ? array_filter($formData['filter_region_condition']) : $formData['filter_region_condition'];
        // 当前配置id信息
        if (empty($formData['id'])) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有传递id信息!";
            echo json_encode($returnData);
            exit;
        } else {
            $formData['id'] = intval($formData['id']);
        }
        
        // 网站id
        if (!empty($formData['site_id'])) {
            $formData['site_id'] = intval($formData['site_id']);
            $flag = Yii::$app->authority->judgeMarketEmailHaveSite($formData['site_id']);
            if (!$flag) {
                $returnData['status'] = 1;
                $returnData['errorInfo'] = "你的账号该没有权限!";
                echo json_encode($returnData);
                exit;
            }
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择所属网站";
            echo json_encode($returnData);
            exit;
        }
        
        // 所使用的模板id
        if (!empty($formData['template_id'])) {
            $formData['template_id'] = intval($formData['template_id']);
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择邮件模板";
            echo json_encode($returnData);
            exit;
        }
        
        // 选择计划发送的时间点（北京时间）
        if (!empty($formData['plan_send_time'])) {
            // 计算所选时间距当天时间0时0分0秒的秒数
            $curBjTime = \yii::$app->bjTime->time();
            $curDate = \yii::$app->bjTime->date('Y-m-d');
            $curDayStart = \yii::$app->bjTime->strtotime($curDate." 00:00:00");
            $planSendTimeInt = yii::$app->bjTime->strtotime($curDate . $formData['plan_send_time']. ":00") - $curDayStart;
            $formData['plan_send_time'] = $planSendTimeInt;
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择计划发送时间";
            echo json_encode($returnData);
            exit;
        }
        
        // 发送方式 0代表网站推送数据当日发送，1以网站推送时间延后发送，2以用户加购时间延后发送
        $formData['send_mode_id'] = !empty($formData['send_mode_id']) ?  intval($formData['send_mode_id']) : 0; 
        
        // 发送延迟时间
        if ($formData['send_mode_id'] == 0) {
            $formData['delay_day'] = 0;
        } else {
            $formData['delay_day'] = !empty($formData['send_mode_day']) ?  intval($formData['send_mode_day']) : 0;
        }
        
        
        $formData['filter_con'] = []; // 过滤条件
        
        // 过滤条件处理
        if (isset($formData['select_filter_condition']) && count($formData['select_filter_condition']) > 0) {
            foreach ($formData['select_filter_condition'] as $selectFilterConditionKey => $selectFilterConditionVal) {
                    if ($selectFilterConditionVal != 0) {
                        $formData['filter_con'][$selectFilterConditionKey]['template_id'] = $selectFilterConditionVal;
                        $formData['filter_con'][$selectFilterConditionKey]['delay'] = isset($formData['select_filter_condition_day'][$selectFilterConditionKey]) ? intval($formData['select_filter_condition_day'][$selectFilterConditionKey]) : 0;
                    }
            } 
        }
        
        $formData['filter_con'] = json_encode($formData['filter_con']);
        $formData['select_region_condition'] = json_encode($formData['select_region_condition']);
        $formData['filter_region_condition'] = json_encode($formData['filter_region_condition']);
         // 过滤退订邮件类型
        if (!empty($formData['must_received'])) {
            $formData['must_received'] = json_encode($formData['must_received']);
        } else {
            $formData['must_received'] = json_encode([]);
        }
        // 接口格式
        $formData['data_format'] = addslashes($formData['data_format']);
        /********************** 处理数据结束 ***************/
        
        // 往数据库中插入数据
        $mailAutoCreate = new MailAutoCreate();
        $mailAutoCreate->updateAutoCreate($formData);
        
        $returnData['status'] = 0;
        $returnData['errorInfo'] = "success";
        echo json_encode($returnData);
        exit;
    }
    
    /**
     * 提交新增配置
     */
    public function actionDoAddAjax()
    {
       
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        
        // 返回信息
        $returnData = [];
        /********************** 处理数据开始 ***************/
        // 国家筛选
        $formData['select_region_condition'] = is_array($formData['select_region_condition']) ? array_filter($formData['select_region_condition']) : $formData['select_region_condition'];
        $formData['filter_region_condition'] = is_array($formData['filter_region_condition']) ? array_filter($formData['filter_region_condition']) : $formData['filter_region_condition'];
//        var_dump($formData['select_region_condition']);exit;
        
        // 网站id
        if (!empty($formData['site_id'])) {
            $formData['site_id'] = intval($formData['site_id']);
            $flag = Yii::$app->authority->judgeMarketEmailHaveSite($formData['site_id']);
            if (!$flag) {
                $returnData['status'] = 1;
                $returnData['errorInfo'] = "你的账号该没有权限!";
                echo json_encode($returnData);
                exit;
            }
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择所属网站";
            echo json_encode($returnData);
            exit;
        }
        
        // 所使用的模板id
        if (!empty($formData['template_id'])) {
            $formData['template_id'] = intval($formData['template_id']);
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择邮件模板";
            echo json_encode($returnData);
            exit;
        }
        
        // 选择计划发送的时间点（北京时间）
        if (!empty($formData['plan_send_time'])) {
            // 计算所选时间距当天时间0时0分0秒的秒数
            $curBjTime = \yii::$app->bjTime->time();
            $curDate = \yii::$app->bjTime->date('Y-m-d');
            $curDayStart = \yii::$app->bjTime->strtotime($curDate." 00:00:00");
            $planSendTimeInt = yii::$app->bjTime->strtotime($curDate . $formData['plan_send_time']. ":00") - $curDayStart;
            $formData['plan_send_time'] = $planSendTimeInt;
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择计划发送时间";
            echo json_encode($returnData);
            exit;
        }
        
        // 发送方式 0代表网站推送数据当日发送，1以网站推送时间延后发送，2以用户加购时间延后发送
        $formData['send_mode_id'] = !empty($formData['send_mode_id']) ?  intval($formData['send_mode_id']) : 0; 
        
        // 发送延迟时间
        if ($formData['send_mode_id'] == 0) {
            $formData['delay_day'] = 0;
        } else {
            $formData['delay_day'] = !empty($formData['send_mode_day']) ?  intval($formData['send_mode_day']) : 0;
        }
        
        
        $formData['filter_con'] = []; // 过滤条件
        
        // 过滤条件处理
        if (isset($formData['select_filter_condition']) && count($formData['select_filter_condition']) > 0) {
            foreach ($formData['select_filter_condition'] as $selectFilterConditionKey => $selectFilterConditionVal) {
                if ($selectFilterConditionVal != 0) {
                    $formData['filter_con'][$selectFilterConditionKey]['template_id'] = $selectFilterConditionVal;
                    $formData['filter_con'][$selectFilterConditionKey]['delay'] = isset($formData['select_filter_condition_day'][$selectFilterConditionKey]) ? intval($formData['select_filter_condition_day'][$selectFilterConditionKey]) : 0;
                } 
            } 
        }
        
        $formData['filter_con'] = json_encode($formData['filter_con']);
        $formData['select_region_condition'] = json_encode($formData['select_region_condition']);
        $formData['filter_region_condition'] = json_encode($formData['filter_region_condition']);
        
       
        // 过滤退订邮件类型
        if (!empty($formData['must_received'])) {
            $formData['must_received'] = json_encode($formData['must_received']);
        } else {
            $formData['must_received'] = json_encode([]);
        }
        
        // 接口格式
        $formData['data_format'] = addslashes($formData['data_format']);
        /********************** 处理数据结束 ***************/
        
        // 往数据库中插入数据
        $mailAutoCreate = new MailAutoCreate();
        $mailAutoCreate->addAutoCreate($formData);
        
        $returnData['status'] = 0;
        $returnData['errorInfo'] = "success";
        echo json_encode($returnData);
        exit;
    }
    
    /*
     * 异步添加专享价配置
     * @user:liugang
     * $date:2018-3-14
     */
    public function actionDoExclusivePriceAddAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        $site_id = $formData['site_id'] ? intval($formData['site_id']) : 0;
        $template_id = $formData['template_id'] ? intval($formData['template_id']) : 0;
        $region_id = $formData['region_id'] ? intval($formData['region_id']) : 0;
        $price_discount = $formData['price_discount'] ? floatval($formData['price_discount']) : 0;
        $valid_days = $formData['valid_days'] ? intval($formData['valid_days']) : 0;
        $channel_id = $formData['channel_id'] ? intval($formData['channel_id']) : 0;
        // 返回信息
        $returnData = [];
        
        if(!$site_id || !$template_id || !$channel_id) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择所属网站,模板,应用渠道";
            echo json_encode($returnData);
            exit;
        }
        
        // 获取模板类型名称
        $template = new MailTemplate();
        $dataTmp = $template->getTemplateTypeById($template_id);
        
        $session = Yii::$app->session;
        // 数据写入数据库
        $insertData = [
            'site_id'   => $site_id,
            'template_id'  => $template_id,
            'channel_id'   => $channel_id,
            'region_id'   => $region_id,
            'price_discount'   => $price_discount,
            'valid_days'   => $valid_days,
            'updater'    => $session['admin_info']['username'],
            'template_type'   => $dataTmp['template_type_compaign'],
        ];
        
        $exclusivePriceConfig = new ExclusivePriceConfig();
        if($exclusivePriceConfig->addData($insertData)) {
            
            $insertId = Yii::$app->db->getLastInsertID();
            
            // 将同一站点，同一模板id的其他配置置为已删除
            $exclusivePriceConfig->updateOtherDel($site_id,$template_id,$insertId);
            
            // 添加启动日志
            $exclusivePriceOperLog = new ExclusivepriceOperLog();

            $log['oper'] = $session['admin_info']['username'];
            $log['operation'] = '新增配置';
            $log['site_name'] = isset(Yii::$app->params['sites']['marketing_email']['web_index'][$site_id]) ? Yii::$app->params['sites']['marketing_email']['web_index'][$site_id] : '';
            $log['template_id'] = $template_id;
            $exclusivePriceOperLog->addLog($log);
            
            $returnData['status'] = 0;
            $returnData['errorInfo'] = "success";
            echo json_encode($returnData);
            exit;
            
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "添加配置失败";
            echo json_encode($returnData);
            exit;
        }
        
    }

    /*
     * 异步编辑专享价配置
     * @user:liugang
     * $date:2018-3-14
     */
    public function actionDoExclusivePriceEditAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $formData = Yii::$app->request->$mothod();
        $config_id = $formData['config_id'] ? intval($formData['config_id']) : 0;
        $site_id = $formData['site_id'] ? intval($formData['site_id']) : 0;
        $template_id = $formData['template_id'] ? intval($formData['template_id']) : 0;
        $region_id = $formData['region_id'] ? intval($formData['region_id']) : 0;
        $price_discount = $formData['price_discount'] ? floatval($formData['price_discount']) : 0;
        $valid_days = $formData['valid_days'] ? intval($formData['valid_days']) : 0;
        $channel_id = $formData['channel_id'] ? intval($formData['channel_id']) : 0;
        // 返回信息
        $returnData = $changeItems = [];
        
        if(!$config_id || !$site_id || !$template_id || !$channel_id) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "请选择所属网站,模板,应用渠道";
            echo json_encode($returnData);
            exit;
        }
        
        $exclusivePriceConfig = new ExclusivePriceConfig();
        
        // 获取当前要编辑的配置信息
        $configInfo = $exclusivePriceConfig->getOne($config_id);
        if(!$configInfo) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "配置信息不存在";
            echo json_encode($returnData);
            exit;
        }
        
        // 判断当前有没有修改的数据
        if(($site_id == $configInfo['site_id']) && ($template_id == $configInfo['template_id']) && ($region_id == $configInfo['region_id']) && ($price_discount == $configInfo['price_discount']) && ($valid_days == $configInfo['valid_days']) && ($channel_id == $configInfo['channel_id'])) {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "没有修改任何信息";
            echo json_encode($returnData);
            exit;
        }
        
        // 记录被更改的数据
        if($region_id != $configInfo['region_id']) {
            $changeItems['region_id'] = $region_id;
        }
        if($price_discount != $configInfo['price_discount']) {
            $changeItems['price_discount'] = $price_discount;
        }
        if($valid_days != $configInfo['valid_days']) {
            $changeItems['valid_days'] = $valid_days;
        }
        if($channel_id != $configInfo['channel_id']) {
            $changeItems['channel_id'] = $channel_id;
        }
        
        
        // 获取模板类型名称
        $template = new MailTemplate();
        $dataTmp = $template->getTemplateTypeById($template_id);
        
        $session = Yii::$app->session;
        // 数据写入数据库
        $insertData = [
            'site_id'   => $site_id,
            'template_id'  => $template_id,
            'channel_id'   => $channel_id,
            'region_id'   => $region_id,
            'price_discount'   => $price_discount,
            'valid_days'   => $valid_days,
            'updater'    => $session['admin_info']['username'],
            'template_type'   => $dataTmp['template_type_compaign'],
        ];
        
        
        if($exclusivePriceConfig->addData($insertData)) {
            
            $insertId = Yii::$app->db->getLastInsertID();
            
            // 将同一站点，同一模板id的其他配置置为已删除
            $exclusivePriceConfig->updateOtherDel($site_id,$template_id,$insertId);
            
            // 添加启动日志
            $exclusivePriceOperLog = new ExclusivepriceOperLog();

            $log['oper'] = $session['admin_info']['username'];
            $log['operation'] = '编辑配置';
            $log['site_name'] = isset(Yii::$app->params['sites']['marketing_email']['web_index'][$site_id]) ? Yii::$app->params['sites']['marketing_email']['web_index'][$site_id] : '';
            $log['template_id'] = $template_id;
            $log['change_items'] = $changeItems ? json_encode($changeItems) : '';
            $exclusivePriceOperLog->addLog($log);
            
            $returnData['status'] = 0;
            $returnData['errorInfo'] = "success";
            echo json_encode($returnData);
            exit;
            
        } else {
            $returnData['status'] = 1;
            $returnData['errorInfo'] = "添加配置失败";
            echo json_encode($returnData);
            exit;
        }
        
    }
    /**
     * 获取某一网站启用的自动触发的邮件模版
     */
    public function actionGetTemplateInfoAjax()
    {
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        $formData = Yii::$app->request->$mothod();
        
        if (!empty($formData['site_id'])) {
            $template = new \app\models\marketing\MailTemplate();
            
            // 获取自动触发邮件的类型模版
            $dataTmp = $template->getAutoTriggerTemplateDetailBySite($formData['site_id']);
            $data = [];
            $data['auto_trigger_template'] = [];
            foreach ($dataTmp as $dataK => $dataV) {
                $data['auto_trigger_template'][$dataK]['template_id'] = $dataV['template_id'];
                $data['auto_trigger_template'][$dataK]['template_name'] = $dataV['template_name'];
            }
            
            // 获取所有启用状态的模版
            $dataAllTmp = $template->getTemplateDetailBySite($formData['site_id']);
            $data['all_template'] = [];
            foreach ($dataAllTmp as $dataK => $dataV) {
                $data['all_template'][$dataK]['template_id'] = $dataV['template_id'];
                $data['all_template'][$dataK]['template_name'] = $dataV['template_name'];
            }
            
            // 获取当前网站的自动触发邮件类型
            $dataAutoType = $template->getAutoTriggerTemplateType($formData['site_id']);
            $data['auto_type'] = [];
            foreach ($dataAutoType  as $dataK => $dataV) {
                $data['auto_type'][$dataK]['id'] = $dataV['id'];
                $data['auto_type'][$dataK]['template_type_name'] = $dataV['template_type_name'];
            }
        } else {
            $data = [];
        }
        echo json_encode($data);
        exit; 
    }
    
    
}